package com.lmf.cmsservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmf.cmsservice.entity.CrmBanner;
import com.lmf.cmsservice.entity.vo.BannerQuery;
import com.lmf.cmsservice.service.CrmBannerService;
import com.lmf.resultCode.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author lmf
 * @since 2022-02-01
 */
@Api(description = "后台banner，幻灯片管理器")
@CrossOrigin
@RestController
@RequestMapping("/cmsservice/bannerAdmin")
public class BannerAdminController  {

    @Autowired
    private CrmBannerService crmBannerService;

    @ApiOperation("分页查询所有幻灯片banner")
    @PostMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable Long page, @PathVariable Long limit){
        Page<CrmBanner> bannerPage = new Page<>();
        bannerPage.setSize(limit);
        bannerPage.setCurrent(page);
        crmBannerService.page(bannerPage, null);
        return R.ok().data("items",bannerPage.getRecords()).data("total",bannerPage.getTotal());
    }

    @ApiOperation("添加banner")
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = crmBannerService.save(crmBanner);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("根据id，修改banner")
    @PostMapping("/updateBanner")
    public R updateBannerById(@RequestBody CrmBanner crmBanner){
        boolean flag = crmBannerService.updateById(crmBanner);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("根据id，删除banner")
    @DeleteMapping("/deleteBanner/{bannerId}")
    public R deleteBannerById(@PathVariable String bannerId){
        boolean flag = crmBannerService.removeById(bannerId);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("根据id，查询banner")
    @GetMapping("/getBannerById/{bannerId}")
    public R getBannerById(@PathVariable String bannerId){
        CrmBanner crmBanner = crmBannerService.getById(bannerId);
        return R.ok().data("item",crmBanner);
    }


}

